<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{active: index === activeIndex}"
        @click="changeRole(index)"
      >
        <div class="role-info">
          <SvgIcon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}

        </div>
        <div class="more">
          <SvgIcon icon-class="more" />
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">
        添加角色
      </el-button>
    </div>

    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in disableTreeList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                ref="tree"
                :data="item.children"
                show-checkbox
                default-expand-all
                node-key="id"
                :highlight-current="false"
                :props="{ label: 'title' }"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="user">
          <div class="user-wrapper">
            <el-table
              :data="roleUserList"
              style="width: 100%"
            >
              <el-table-column
                type="index"
                width="250"
                label="序号"
              />
              <el-table-column
                prop="name"
                label="员工姓名"
              />
              <el-table-column
                prop="userName"
                label="登录账号"
              />
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRoleList, getTreeData, getRoleDetailAPI, getRoleMemberList } from '@/api/Role'
import SvgIcon from '@/components/SvgIcon'

export default {
  name: 'RoleVue',
  components: {
    SvgIcon
  },
  data () {
    return {
      roleList: [], // 角色列表
      activeIndex: 0, // 当前选中角色
      treeList: [], // 树形结构数据
      disableTreeList: [], // 禁用的tree列表
      activeName: 'tree', // 默认打开tree
      roleUserList: [] // 成员列表
    }
  },
  async mounted () {
    await this.getRoleList() // 获取角色列表
    await this.getTreeList() // 获取树形数据结构
    this.getRoleDetail(this.roleList[0].roleId) // 获取当前角色详情
    this.getRoleMemberList(this.roleList[0].roleId) // 获取成员列表
  },
  methods: {
    // 获取角色列表
    async getRoleList () {
      const res = await getRoleList()
      console.log(res)
      this.roleList = res.data
    },
    // 切换角色
    changeRole (idx) {
      this.activeIndex = idx
      // 获取当前角色下的权限点数据列表
      const roleId = this.roleList[idx].roleId
      console.log(roleId)
      this.getRoleDetail(roleId)
      // 获取当前角色下的成员列表
      this.getRoleMemberList(roleId)
    },
    // 获取树形结构数据
    async getTreeList () {
      const res = await getTreeData()
      this.treeList = res.data
      // 处理禁用
      this.disableTreeList = JSON.parse(JSON.stringify(res.data))
      this.addDisabled(this.disableTreeList)
    },
    // 禁用功能
    addDisabled (treeList) {
      treeList.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    // 获取当前角色详情
    async getRoleDetail (roleId) {
      const res = await getRoleDetailAPI(roleId)
      this.perms = res.data.perms
      // 高亮权限列表
      const treeComponentList = this.$refs.tree
      if (treeComponentList && Array.isArray(treeComponentList)) {
        treeComponentList.forEach((tree, index) => {
          tree.setCheckedKeys(this.perms[index])
        })
      } else {
        console.warn('this.$refs.tree is not defined or not an array')
      }
    },
    // 获取成员列表
    async getRoleMemberList (roleId) {
      const res = await getRoleMemberList(roleId)
      this.roleUserList = res.data.rows
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
        // font-size: 16px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>
